package com.micro.school.order.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.micro.school.common.core.constant.PayConstants;
import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.utils.SecurityContextHolderUtils;
import com.micro.school.common.core.utils.StringUtils;
import com.micro.school.common.core.utils.poi.ExcelUtil;
import com.micro.school.common.core.web.controller.BaseController;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.common.core.web.page.TableDataInfo;
import com.micro.school.common.log.annotation.Log;
import com.micro.school.common.log.enums.BusinessType;
import com.micro.school.common.security.utils.SecurityUtils;
import com.micro.school.order.bo.PayStudentFeeBo;
import com.micro.school.order.service.ICollegeService;
import com.micro.school.order.vo.PayStudentFeeVo;
import com.micro.school.system.api.RemoteUserService;
import com.micro.school.system.api.domain.StudentInfos;
import com.micro.school.system.api.domain.SysUser;
import com.micro.school.system.api.domain.vo.PayStudentFeeItemVos;
import com.micro.school.system.api.domain.vo.PayStudentFeePaymentDetailVos;
import com.micro.school.system.api.domain.vo.PayStudentFeeVos;
import com.micro.school.system.api.domain.vo.StudentInfoVos;
import com.micro.school.system.api.model.LoginUser;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.micro.school.order.entity.PayStudentFee;
import com.micro.school.order.service.IPayStudentFeeService;

/**
 * 学生收费管理Controller
 * 
 * @author lyb
 * @date 2025-08-16
 */
@RestController
@RequestMapping("/pay/payStudentFee")
public class PayStudentFeeController extends BaseController
{
    @Autowired
    private IPayStudentFeeService payStudentFeeService;

    @Autowired
    private RemoteUserService userService;

    @Autowired
    private ICollegeService collegeService;

    /**
     * 查询学生收费管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PayStudentFeeBo payStudentFee)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        payStudentFee.setTenantId(loginUser.getSysUser().getTenantId().intValue());
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            payStudentFee.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            payStudentFee.setCollegeIds(SecurityContextHolderUtils.getCollegeId() != null && SecurityContextHolderUtils.getCollegeId().length > 0 ? SecurityContextHolderUtils.getCollegeId(): null);
        }
        startPage();
        List<PayStudentFeeVo> list = payStudentFeeService.selectPayStudentFeeList(payStudentFee);
        return getDataTable(list);
    }


    /**
     * 查询学生收费管理列表
     */
    @PostMapping("/getFeignFeeVos")
    public AjaxResult getFeignFeeVos(@RequestBody PayStudentFeeVos payStudentFeeVos)
    {
        PayStudentFeeVos list = payStudentFeeService.getFeignOne(payStudentFeeVos);
        return success(list);
    }


    /**
     * 查询学生收费管理列表
     */
    @PostMapping("/getFeignFeeItemVo")
    public AjaxResult getFeignFeeItemVo(@RequestBody PayStudentFeeVos payStudentFeeVos)
    {
        Map<String, Map<String, Object>> mapMap = payStudentFeeService.getFeignFeeItemVo(payStudentFeeVos);
        return success(mapMap);
    }


    /**
     * 查询学生收费管理列表
     */
    @PostMapping("/getFeignFeeItemDetailVo")
    public AjaxResult queryStudentFeeItemDetails(@RequestBody PayStudentFeeVos payStudentFeeVos)
    {
        List<PayStudentFeeItemVos> payStudentFeeItemVosList = payStudentFeeService.queryStudentFeeItemDetails(payStudentFeeVos);
        return success(payStudentFeeItemVosList);
    }

    /**
     * 导出学生收费管理列表
     */
    @Log(title = "学生收费管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayStudentFeeBo payStudentFee)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        payStudentFee.setTenantId(loginUser.getSysUser().getTenantId().intValue());
        if (!SecurityContextHolder.getUserName().contains("admin")) {
            payStudentFee.setCampusIds(SecurityContextHolderUtils.getCampusId() != null && SecurityContextHolderUtils.getCampusId().length > 0 ? SecurityContextHolderUtils.getCampusId(): null);
            payStudentFee.setCollegeIds(SecurityContextHolderUtils.getCollegeId() != null && SecurityContextHolderUtils.getCollegeId().length > 0 ? SecurityContextHolderUtils.getCollegeId(): null);
        }
        List<PayStudentFeeVo> list = payStudentFeeService.selectPayStudentFeeList(payStudentFee);
        ExcelUtil<PayStudentFeeVo> util = new ExcelUtil<PayStudentFeeVo>(PayStudentFeeVo.class);
        util.exportExcel(response, list, "学生收费管理数据");
    }

    /**
     * 获取学生收费管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(payStudentFeeService.selectPayStudentFeeById(id));
    }




    /**
     * 新增学生收费管理
     */
    @Log(title = "学生收费管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayStudentFeeBo payStudentFeeBo)
    {
        if(StringUtils.isEmpty(payStudentFeeBo.getIdNumber())){
            return AjaxResult.error("身份证不能为空");
        }
        //查询学生
        StudentInfoVos studentInfoVos = new StudentInfoVos();
        studentInfoVos.setIdNumber(payStudentFeeBo.getIdNumber());
        R<StudentInfos> studentR =  userService.queryStudent(studentInfoVos);
        if(null==studentR || null==studentR.getData()){
            return AjaxResult.error("学生不存在");
        }
        StudentInfos student = studentR.getData();
        if(null==student.getName()){
            return AjaxResult.error("学生不存在");
        }
        setStudentInfo(student,payStudentFeeBo);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        payStudentFeeBo.setTenantId(sysUser.getTenantId().intValue());
        payStudentFeeBo.setDeleteFlag(PayConstants.DEL_STR_0);
        payStudentFeeBo.setCreateDept(sysUser.getDeptId());
        payStudentFeeBo.setCreateBy(sysUser.getUserName());
        payStudentFeeBo.setCreateTime(new Date());
        payStudentFeeBo.setUpdateBy(sysUser.getUserName());
        payStudentFeeBo.setUpdateTime(new Date());
        payStudentFeeBo.setCampusId(collegeService.getCampusIdByCollegeId(payStudentFeeBo.getCollegeId()));;
        return payStudentFeeService.insertPayStudentFee(payStudentFeeBo);
    }
    private void setStudentInfo(StudentInfos student,PayStudentFeeBo payStudentFeeBo){
        payStudentFeeBo.setStudentName(student.getName());
        payStudentFeeBo.setStudentId(student.getId().intValue());
//        payStudentFeeBo.setCollegeId(student.getCollegeId());
//        //payStudentFeeBo.setModelId(student.getMo);
//        payStudentFeeBo.setMajorId(student.getMajorId());
//        payStudentFeeBo.setGradeId(student.getGradeId());
    }

    /**
     * 修改学生收费管理
     */
    @Log(title = "学生收费管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayStudentFee payStudentFee)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PayStudentFeeVo feeVo = payStudentFeeService.selectPayStudentFeeById(payStudentFee.getId().longValue());
        if(loginUser.getTenantId().intValue()!=feeVo.getTenantId().intValue()){
            return AjaxResult.error();
        }
        return toAjax(payStudentFeeService.updatePayStudentFee(payStudentFee));
    }

    /**
     * 删除学生收费管理
     */
    @Log(title = "学生收费管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        for(Long id:ids){
            PayStudentFeeVo feeVo = payStudentFeeService.selectPayStudentFeeById(id);
            if (!StringUtils.equals(SecurityContextHolder.getUserTenantId(), String.valueOf(feeVo.getTenantId()))) {
                return AjaxResult.error();
            }
            if(!PayConstants.BILLSTATUS_40.equals(feeVo.getBillStatus())){
                return AjaxResult.error("只允许删除已作废的数据");
            }
        }
        return toAjax(payStudentFeeService.deletePayStudentFeeByIds(ids));
    }
    /**
     * 作废
     */
    @Log(title = "作废", businessType = BusinessType.DELETE)
    @PostMapping(value = "/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PayStudentFee configUpdate = new PayStudentFee();
        configUpdate.setBillStatus(PayConstants.BILLSTATUS_40);
        configUpdate.setTenantId(loginUser.getSysUser().getTenantId().intValue());
        configUpdate.setId(id.intValue());
        configUpdate.setUpdateBy(loginUser.getUsername());
        configUpdate.setUpdateTime(new Date());
        return toAjax(payStudentFeeService.updatePayStudentFee(configUpdate));
    }

    @PostMapping(value = "/updateStudentFee")
    public void updateStudentFee(@RequestBody List<PayStudentFeePaymentDetailVos> payStudentFeePaymentVosDetails)
    {
        payStudentFeeService.updateStudentFee(payStudentFeePaymentVosDetails);
    }

    @PostMapping(value = "/queryStudentFeeByDetail")
    public PayStudentFeeVos queryStudentFeeByDetail(@RequestParam("studentFeeId") Long studentFeeId)
    {
       return payStudentFeeService.queryStudentFeeByDetail(studentFeeId);
    }

    @PostMapping(value = "/sumStudentFee")
    public BigDecimal sumStudentFee(@RequestParam("studentFeeId") Long studentFeeId)
    {
        return payStudentFeeService.sumStudentFee(studentFeeId);
    }


    @PostMapping(value = "/updateStudentFeeTotalMoney")
    public void updateStudentFeeTotalMoney(@RequestParam("studentFeeId") PayStudentFeeVos payStudentFeeVos)
    {
        payStudentFeeService.updateStudentFeeTotalMoney(payStudentFeeVos);
    }


    @PostMapping(value = "/updateAllOrderCollegeName")
    public void updateAllOrderCollegeName(@RequestParam("collegeName")String collegeName,@RequestParam("collegeId") Long collegeId)
    {
        payStudentFeeService.updateAllOrderCollegeName(collegeName,collegeId);
    }

    @PostMapping(value = "/updateAllCollegeMajor")
    public void updateAllCollegeMajor(@RequestParam("majorName")String majorName,@RequestParam("id") Long id)
    {
        payStudentFeeService.updateAllCollegeMajor(majorName,id);
    }

    @PostMapping(value = "/updateAllCollegeModel")
    public void updateAllCollegeModel(@RequestParam("modelName")String modelName,@RequestParam("id") Long id)
    {
        payStudentFeeService.updateAllCollegeModel(modelName,id);
    }

    @PostMapping(value = "/updateAllCampusName")
    public void updateAllCampusName(@RequestParam("campusName")String campusName,@RequestParam("id") Long id)
    {
        payStudentFeeService.updateAllCampusName(campusName,id);
    }

    @PostMapping(value = "/updateAllFeeYear")
    public void updateAllFeeYear(@RequestParam("feeYear")String feeYear,@RequestParam("id") Long id)
    {
        payStudentFeeService.updateAllFeeYear(feeYear,id);
    }

}
